import * as _ from 'lodash/core';
import * as moment from 'moment';
import storageApi from '../../core/storage/storage';

@Inject('$log', 'loginService', '$rootScope', '$state')
class LoginController {
  constructor() {
    $log.debug('Hello from main controller!');
    this.lodash_version = _.VERSION;
    this.moment_version = moment.version;

    // this.data = {
    //   userName: 'rd1baqy1',
    //   password: '88888888',
    // };
    this.data = {};
  }

  // 登录
  userLogin() {
    this.data.startLoginFlag = true;
    const param = {
      userName: this.data.userName,
      password: this.data.password,
    };

    this.loginService.login(param)
      .then((res) => {
        if (res.success) {
          this.data.startLoginFlag = false;
          this.$rootScope.orgId = res.orgId;
          this.$rootScope.userId = res.userId;
          this.$rootScope.userName = res.userName;
          this.$rootScope.orgType = res.orgBean.type;
          this.$rootScope.permissionList = res.permissionList;
          this.$rootScope.isLogined = true;
          storageApi.set('orgId', this.$rootScope.orgId);
          storageApi.set('userId', this.$rootScope.userId);
          storageApi.set('userName', this.$rootScope.userName);
          storageApi.set('orgType', this.$rootScope.orgType);
          storageApi.set('permissionList', this.$rootScope.permissionList);
          if (res.orgBean.type === '1' || res.orgBean.type === '2') {
            this.$state.go('leaderHome');
          } else {
            this.$state.go('index.home');
          }
        } else {
          this.data.startLoginFlag = false;
          this.data.error = true;
          this.$rootScope.isLogined = false;
        }
        this.$log.debug(res);
      });
  }
}

export default LoginController;
